package dst.tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Fully Tested: No
 * Fully Documented: No
 * Created: 20-Nov-2011
 * Last Updated: 20-Nov-2011
 *
 * @author Shimu Wu
 */
public class TreeNode<T> implements Iterable<TreeNode<T>> {

    private T data;

    private List<TreeNode<T>> children;

    public TreeNode(T data) {
        this.data = data;
        this.children = new ArrayList<TreeNode<T>>();
    }

    /**
     * Returns the data contained in this TreeNode
     * @return the data contained in this TreeNode
     */
    public T getData() {
        return data;
    }

    /**
     * Adds the given child node to this TreeNode's list of children
     * @param child the child node to be added to this TreeNode
     */
    public void addChild(TreeNode<T> child) {
        children.add(child);
    }

    /**
     * Adds a new child node containing the given data to this TreeNode. 
     * The new child node is also returned. 
     * 
     * @param childData the data to be stored in the new child node
     * @return a new child node containing the given data.
     */
    public TreeNode<T> addChild(T childData) {
        TreeNode<T> newNode = new TreeNode(childData);
        children.add(newNode);
        return newNode;
    }
    
    /**
     * Returns an iterator over this TreeNode's list of children.
     * @return an iterator over this TreeNode's list of children
     */
    @Override
    public Iterator<TreeNode<T>> iterator() {
        return children.iterator();
    }

    @Override
    public String toString() {
        return String.format("TreeNode (%s)", data.toString());
    }
}
